/* eslint-disable indent */
<template>
  <div class="login_container">
    <div class="form">
      <!-- :rules 验证表单 -->
      <!-- rules: 表单验证 -->
      <h2>登录</h2>
      <el-form ref="loginFormRef" 
        class="login_form" 
        :model="loginForm" 
        :rules="loginFormRules"
        size="large">
        <!-- 用户名 -->
        <!-- prop 指定校验规则 -->
          <el-form-item prop="username">
              <el-input placeholder="用户名" v-model="loginForm.username">
              </el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
              <el-input placeholder="请输入密码" v-model="loginForm.password" show-password></el-input>
              </el-form-item>
          <!-- 按钮 -->
          <el-form-item class="btns"  >
              <el-button type="primary" @click="login">登陆</el-button>
              <el-button type="info" @click="resetLoginForm">重置</el-button>
          </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3到10个字符之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在6到18个字符之间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginForm () {
      // 重置表单
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return

        const { data: res } = await this.$http.post('/login', this.loginForm).catch(err => {
          return this.$message.error(err.message)
        })
        // console.log(data);
        console.log(res);
        if (!res.status) {
          return this.$message.error('登陆失败')
        }
        this.$message.success('登陆成功')
        console.log(res)
        // 将用户验证信息保存到本地
        window.sessionStorage.setItem('user', res.data)
        // 显示主页
        this.$router.push('/admin')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  // background-color: #2b4b6b;
  background: linear-gradient(135deg,#17ead9,#6078ea);
  background-size: 300% 300%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  animation: gradient 5s ease infinite;
}

@keyframes gradient {
  0% {
      background-position: 0% 50%;
  }
  50% {
      background-position: 100% 50%;
  }
  100% {
      background-position: 0% 50%;
  }
}

.form {
    background-color: #fff;
    padding: 50px;
    border-radius: 20px;
    width: 300px;
  }


</style>
